package com.jky.stmall.front.interceptor;

import com.jky.stmall.front.entity.User;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class UserInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //验证用户是否登录
        User user=(User) request.getSession().getAttribute("user");
        //用户未登录
        if (user==null){
            //跳转登陆页面
            response.sendRedirect(("/user/login"));
            //拦截
            return false;
        }
        return true;
    }
}
